package main;

import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.lang.reflect.Constructor;
import javax.swing.JPanel;

/**
 * Class creating the main frame
 * @author Patrick Bauer
 */
public class MainFrame extends JFrame {

    /**
     * Local colorScheme index
     */
    int colorScheme = -1;

    /**
     * Width of the generated frame
     */
    int width = 1280;

    /**
     * Height of the generated frame
     */
    int height = 800;

    /**
     * Instance of random for generating numbers
     */
    Random random = new Random();

    /**
     * Stringarray of available function classes
     */
    String[] availableFunctions = {"powX", "powX3" , "sinX", "X", "cosX", "eX", "onedivX", "onedivX2", "sqrtX", "expX"};

    /**
     * Constructor for mainframe
     * @param lcolorScheme colorscheme to use (-1 for random; defined in ColorScheme class)
     */
    MainFrame(int lcolorScheme) {
        this.setColorScheme(lcolorScheme);
        this.showWindow();
    }

    /**
     * Generates and manages 4 random panels
     */
    private void drawPanels() {
        //set mainPanel
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);
        this.add(mainPanel);

        //generate random panel
        JPanel panel1 = this.getRandomPanel();
        JPanel panel2 = this.getRandomPanel();
        JPanel panel3 = this.getRandomPanel();
        JPanel panel4 = this.getRandomPanel();
        

        //helping variables for panel positioning
        int border = 10;
        int panelWidth = (this.width -  border*2)/2;
        int panelHeight = (this.height - border*4)/2;

        //panel positioning
        panel1.setBounds(0+border,             0+border,              panelWidth-border, panelHeight-border);
        panel2.setBounds(panelWidth+border,    0+border,              panelWidth-border, panelHeight-border);
        panel3.setBounds(0+border,             panelHeight+border,    panelWidth-border, panelHeight-border);
        panel4.setBounds(panelWidth+border,    panelHeight+border,    panelWidth-border, panelHeight-border);

        //add random panels to main panel
        mainPanel.add(panel1);
        mainPanel.add(panel2);
        mainPanel.add(panel3);
        mainPanel.add(panel4);
    }


    /**
     * Sets JFrame options and opens the mainframe
     */
    private void showWindow() {
        this.setIcons();
        this.setTitle("Funktionsgraphen");
        this.setSize(this.width, this.height);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.drawPanels();
        this.setVisible(true);
    }

    /**
     * Loads icons from ressources
     */
    private void setIcons() {
        ArrayList<Image> icons = new ArrayList<Image>();
        icons.add(new ImageIcon(getClass().getResource("/icons/icon_small.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("/icons/icon_big.png")).getImage());
        
        this.setIconImages(icons);
    }

    /**
     * Sets local variable to given colorScheme index
     * @param lcolorScheme Colorscheme for random panels (-1 for random colorScheme)
     */
    private void setColorScheme(int lcolorScheme) {
        if(lcolorScheme == -1)
            lcolorScheme = this.random.nextInt(ColorScheme.colorSchemes.length);
        
        this.colorScheme = lcolorScheme;
    }

    /**
     * Loads and instances a random function-panel class from the functions package
     * Function list in this.availableFunctions[]
     * @return random Panel
     */
    private JPanel getRandomPanel() {
        JPanel panel = null;

        //random array index
        int function = this.random.nextInt(this.availableFunctions.length);


        //trys to load and instances the class
        try {
            Class cl = Class.forName("functions."+this.availableFunctions[function]);
            Constructor con = cl.getConstructor(int.class);
            panel = (JPanel) con.newInstance(this.colorScheme);

        //exit on error
        } catch (Exception e) {
            System.out.println(e.toString());
            //System.out.println("Kritischer Fehler, Klasse \"" + this.availableFunctions[function] + "\" nicht gefunden");
            System.exit(1);
        }
                        
        return panel;
    }
    
}
